<?php
/**
 * Command line parser
 *
 * @version 0.2a
 */

// No direct access
defined( "__DEENZO__" ) or die( "deenzo$" ); 

function cmdl( $argv ) {
    
    /** Support for strings delimited by quotes */
    for ( $i = 0; $i < strlen( $argv ); $i++, $b = $e = -1 ) {
        $chr = substr( $argv, $i, 1 );
        
        if ( preg_match( "/(\"|\')/", $chr, $q )
        && ( substr( $argv, $i - 1, 1 ) != "\\" ) ) {
           
            $e = $b = $i + 1; $q = $q[0];
            
            do {
                $e  = strpos( $argv, $q, $e + 1 );
                
            } while ( substr( $argv, $e - 1, 1 ) == "\\" );
            
           
            if ( substr( $argv, $e, 1 ) != $q )
                $e = strlen( $argv ) - 1;
            
            
            $n      = count( $varz );
            $varz[] = ( $str = substr( $argv, $b, $e - $b ) );
            
            $argv   = str_replace( "$q$str$q", '$' . $n, $argv );
        }
    }
    
    
    $chr0 = substr( $argv, 0, 1 );
    $argv = preg_split( "/(^\-\-?|\s\-\-?)/", $argv );

    /** Parsing ARGV */
    foreach ( $argv as $x ) {
       
        if ( $x ) {
            list( $y, $z ) = preg_split( "/\s/", $x );
            
            if ( $y && ( $z || is_numeric( $z ) ) )
                $cmdl[ $y ]     = $z;
            
            elseif ( $y ) {
                if ( ( count( $cmdl ) == 0 ) 
                  && ( $chr0 != "-" ) )
                    $cmdl[0]    = $y;
                else
                    $cmdl[ $y ] = true;
            }
        }
    } 
    
    foreach( $cmdl as $k => $v ) {
        if ( substr( $v, 0, 1 ) == '$' )
            $cmdl[ $k ] = $varz[ substr( $v, 1 ) ];
    }
    
    return $cmdl;
}